/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.bocai.dahuatech.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.MyExchangeListBean;
import com.bocai.dahuatech.R;
import com.bumptech.glide.Glide;

import java.util.List;

public class MyExchangeAdapterHolder extends RecyclerView.Adapter<MyExchangeAdapterHolder.ViewHolder> {

    OnItemClickListener mItemClickListener;
    private Context context;
    List<MyExchangeListBean.ContentEntity> datas;
    public MyExchangeAdapterHolder(Context context,List<MyExchangeListBean.ContentEntity> datas) {
        this.context = context;
        this.datas=datas;
    }
    public void addDatas(List<MyExchangeListBean.ContentEntity> list){
        datas.addAll(list);
        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        final View sView = mInflater.inflate(R.layout.myexchange_item, parent, false);
        return new ViewHolder(sView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tvTime.setText(datas.get(position).getFCreateTime());
        if(datas.get(position).isFExchange())
        {

            holder.tvState.setText(R.string.alreadyexchange);
        }else{
            holder.tvState.setText(R.string.ordertotrue);
        }
        holder.tvName.setText(datas.get(position).getFItemName());
        holder.tvMode.setText(datas.get(position).getFOutModel());
        holder.tvPrice.setText(context.getString(R.string.money)+datas.get(position).getFPrice());
        holder.tvPoint.setText(datas.get(position).getFCoin() + context.getResources().getString(R.string.huapoint));
        holder.tvCount.setText(context.getResources().getString(R.string.x)+datas.get(position).getFNum());
        holder.tvTotalCount.setText(context.getString(R.string.gong)+datas.get(position).getFNum()+context.getResources().getString(R.string.productcount));
        holder.tvTotalPrice.setText(context.getString(R.string.money)+datas.get(position).getFTotal()+context.getResources().getString(R.string.huapoint));
        Glide.with(context).load(datas.get(position).getFImgSrc())
                .into(holder.ivPic);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void SetOnItemClickListener(final OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvTime,tvState,tvName,tvMode,tvPrice,tvPoint,tvCount,tvTotalCount,tvTotalPrice;
        ImageView ivPic;
        public ViewHolder(View view) {
            super(view);
            tvCount= (TextView) view.findViewById(R.id.tv_myexchange_count);
            tvTime= (TextView) view.findViewById(R.id.tv_myexchange_time);
            tvState= (TextView) view.findViewById(R.id.tv_myexchange_state);
            tvName= (TextView) view.findViewById(R.id.tv_myexchange_name);
            tvMode= (TextView) view.findViewById(R.id.tv_myexchange_mode);
            tvPoint= (TextView) view.findViewById(R.id.tv_myexchange_point);
            tvPrice= (TextView) view.findViewById(R.id.tv_myexchange_price);
            tvTotalCount= (TextView) view.findViewById(R.id.tv_myexchange_totalcount);
            tvTotalPrice= (TextView) view.findViewById(R.id.tv_myexchange_totalprice);
            ivPic= (ImageView) view.findViewById(R.id.iv_myexchange_pic);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(v, getLayoutPosition());
            }
        }

    }

}
